/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openbookformat.project.newprojectwizard;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.openide.util.NbBundle;

public final class NewOBFProjectVisualPanel3 extends JPanel {
    private String testPageContents = null;
    
    public static final String PROP_PAGE_PATTERN = "pagePattern";
    public static final String PROP_HEADINGS_PATTERN = "headingsPattern";
    public static final String PROP_USE_PAGE_GROUP = "UsePageGroup";
    public static final String PROP_PAGE_GROUP = "pageGroup";
    public static final String PROP_USE_HEADINGS_GROUP = "useHeadingsGroup";
    public static final String PROP_HEADINGS_GROUP = "headingsGroup";
    public static final String PROP_USE_AUTOLEVEL = "useAutoLevel";
    public static final String PROP_LEVEL_GROUP = "levelGroup";

    
    /** Creates new form NewOBFProjectVisualPanel3 */
    public NewOBFProjectVisualPanel3() {
        initComponents();
    }

    @Override
    public String getName() {
        return NbBundle.getMessage(NewOBFProjectVisualPanel3.class, 
                "NewOBFProjectVisualPanel3.Name");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPagePattern = new javax.swing.JTextArea();
        jTestPagePattern = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jHeadingsPatternScroll = new javax.swing.JScrollPane();
        jHeadingsPattern = new javax.swing.JTextArea();
        jTestHeadingPattern = new javax.swing.JButton();
        jUseHeadingGroup = new javax.swing.JCheckBox();
        jHeadingGroup = new javax.swing.JSpinner();
        jUsePageGroup = new javax.swing.JCheckBox();
        jPageGroup = new javax.swing.JSpinner();
        jAutoLevel = new javax.swing.JCheckBox();
        jLevelGroup = new javax.swing.JSpinner();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel3.class, "NewOBFProjectVisualPanel3.jLabel1.text")); // NOI18N

        jPagePattern.setColumns(20);
        jPagePattern.setRows(5);
        jScrollPane1.setViewportView(jPagePattern);

        org.openide.awt.Mnemonics.setLocalizedText(jTestPagePattern, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel3.class, "NewOBFProjectVisualPanel3.jTestPagePattern.text")); // NOI18N
        jTestPagePattern.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTestPagePatternActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel3.class, "NewOBFProjectVisualPanel3.jLabel2.text")); // NOI18N

        jHeadingsPattern.setColumns(20);
        jHeadingsPattern.setRows(5);
        jHeadingsPattern.setText(org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel3.class, "NewOBFProjectVisualPanel3.jHeadingsPattern.text")); // NOI18N
        jHeadingsPatternScroll.setViewportView(jHeadingsPattern);

        org.openide.awt.Mnemonics.setLocalizedText(jTestHeadingPattern, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel3.class, "NewOBFProjectVisualPanel3.jTestHeadingPattern.text")); // NOI18N
        jTestHeadingPattern.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTestHeadingPatternActionPerformed(evt);
            }
        });

        jUseHeadingGroup.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(jUseHeadingGroup, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel3.class, "NewOBFProjectVisualPanel3.jUseHeadingGroup.text")); // NOI18N
        jUseHeadingGroup.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jUseHeadingGroupStateChanged(evt);
            }
        });

        jHeadingGroup.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(2), Integer.valueOf(1), null, Integer.valueOf(1)));

        jUsePageGroup.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(jUsePageGroup, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel3.class, "NewOBFProjectVisualPanel3.jUsePageGroup.text")); // NOI18N
        jUsePageGroup.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jUsePageGroupStateChanged(evt);
            }
        });

        jPageGroup.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));

        jAutoLevel.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(jAutoLevel, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel3.class, "NewOBFProjectVisualPanel3.jAutoLevel.text")); // NOI18N

        jLevelGroup.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jUsePageGroup)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPageGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 123, Short.MAX_VALUE)
                                .addComponent(jTestPagePattern))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jHeadingsPatternScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jUseHeadingGroup)
                                    .addComponent(jAutoLevel))
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLevelGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jHeadingGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                                        .addComponent(jTestHeadingPattern)))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTestPagePattern)
                    .addComponent(jUsePageGroup)
                    .addComponent(jPageGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jHeadingsPatternScroll, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTestHeadingPattern)
                    .addComponent(jUseHeadingGroup)
                    .addComponent(jHeadingGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jAutoLevel)
                    .addComponent(jLevelGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTestPagePatternActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTestPagePatternActionPerformed
        String match1 = "";
        String match2 = "";
        
        Pattern pattern = Pattern.compile(getPagePattern());
        Matcher matcher = pattern.matcher(NewOBFProjectUtil.html2Text(testPageContents));
        if(matcher.find()) {
            if(jUsePageGroup.isSelected()) {
                match1 = matcher.group((Integer) jPageGroup.getValue());
            } else {
                match1 = matcher.group();
            }
        }
        
        while(matcher.find()) {
            if(jUsePageGroup.isSelected()) {
                match2 = matcher.group((Integer) jPageGroup.getValue());
            } else {
                match2 = matcher.group();
            }   
        }
        JOptionPane.showMessageDialog(this, "First Match: " + match1 
                + "\nLast Match: " + match2 );                    
    }//GEN-LAST:event_jTestPagePatternActionPerformed

    private void jTestHeadingPatternActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTestHeadingPatternActionPerformed
        String msg = "";
        int max = 7; //number of heading to show
        Pattern pattern = Pattern.compile(getHeadingsPattern());
        Matcher matcher = pattern.matcher(testPageContents);
        
        int i = 0;
        while(matcher.find() && i++ < max) {
            if(jUseHeadingGroup.isSelected()) {
                msg += matcher.group((Integer) jHeadingGroup.getValue()) + "\n";
            } else {
                msg += matcher.group() + "\n";
            }
        }
        
        JOptionPane.showMessageDialog(this, msg);
    }//GEN-LAST:event_jTestHeadingPatternActionPerformed

    private void jUseHeadingGroupStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jUseHeadingGroupStateChanged
        jHeadingGroup.setEnabled(jUseHeadingGroup.isSelected());
        jAutoLevel.setEnabled(jUseHeadingGroup.isSelected());
        jLevelGroup.setEnabled(jUseHeadingGroup.isSelected());
    }//GEN-LAST:event_jUseHeadingGroupStateChanged

    private void jUsePageGroupStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jUsePageGroupStateChanged
        jPageGroup.setEnabled(jUsePageGroup.isSelected());
    }//GEN-LAST:event_jUsePageGroupStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jAutoLevel;
    private javax.swing.JSpinner jHeadingGroup;
    private javax.swing.JTextArea jHeadingsPattern;
    private javax.swing.JScrollPane jHeadingsPatternScroll;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSpinner jLevelGroup;
    private javax.swing.JSpinner jPageGroup;
    private javax.swing.JTextArea jPagePattern;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jTestHeadingPattern;
    private javax.swing.JButton jTestPagePattern;
    private javax.swing.JCheckBox jUseHeadingGroup;
    private javax.swing.JCheckBox jUsePageGroup;
    // End of variables declaration//GEN-END:variables

    public void setTestPageContents(String testPageContents) {
        this.testPageContents = testPageContents;
    }
    
    public String getPagePattern() {
        return jPagePattern.getText();
    }
    
    public void setHeadingsPattern(String pagePattern) {
        String oldPagePattern = getPagePattern();
        jPagePattern.setText(pagePattern);
        propertyChangeSupport.firePropertyChange(PROP_PAGE_PATTERN, oldPagePattern, pagePattern);
    }

    public String getHeadingsPattern() {
        return jHeadingsPattern.getText();
    }

    public void setPagePattern(String headingsPattern) {
        String oldHeadingsPattern = getPagePattern();
        jPagePattern.setText(headingsPattern);
        propertyChangeSupport.firePropertyChange(PROP_HEADINGS_PATTERN, oldHeadingsPattern, headingsPattern);
    }

    
    /**
     * Get the value of headingsGroup
     *
     * @return the value of headingsGroup
     */
    public int getHeadingsGroup() {
        return (Integer) jHeadingGroup.getValue();
    }

    
    /**
     * Get the value of useHeadingsGroup
     *
     * @return the value of useHeadingsGroup
     */
    public boolean isUseHeadingsGroup() {
        return jUseHeadingGroup.isSelected();
    }

    
    /**
     * Get the value of pageGroup
     *
     * @return the value of pageGroup
     */
    public int getPageGroup() {
        return (Integer) jPageGroup.getValue();
    }


    /**
     * Get the value of UsePageGroup
     *
     * @return the value of UsePageGroup
     */
    public boolean isUsePageGroup() {
        return jUsePageGroup.isSelected();
    }
    
    public boolean isAutoLevel() {
        return jAutoLevel.isSelected();
    }
    
    public int getLevelGroup() {
        return (Integer) jLevelGroup.getValue();
    }

    
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

}
